{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d905252",
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]\n",
    "\n",
    "res = itertools.accumulate(data, lambda x, y:x+y)\n",
    "print(list(res))\n",
    "# [3, 7, 13, 15, 16, 25, 25, 32, 37, 45]"
   ]
  },
  {
   "cell_type": "raw",
   "id": "0c6a1cb7",
   "metadata": {},
   "source": [
    "函数compress()可以根据我们的喜好来过滤内容。与函数filter()函数不同，函数compress()需要传入相应的标志位来决定是否应该保留每个值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cff5404",
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]\n",
    "selector = [1, 0, 0, 0, 1, 1, 0, 1, 1, 0]\n",
    "res = itertools.compress(data, selector)\n",
    "print(list(res))\n",
    "# [3, 1, 9, 7, 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7ee7f8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "data = [('New York', 'US'), (\"Shanghai\", \"China\"), (\"LA\", 'US'), (\"Chongqing\", \"China\")]\n",
    "\n",
    "for city, group in itertools.groupby(sorted(data, key=lambda x: x[1]), lambda x: x[1]):\n",
    "    for i in group:\n",
    "        print(\"%s is in %s.\" % (i[0], city))\n",
    "    \n",
    "    print()\n",
    "    \n",
    "# Shanghai is in China.\n",
    "# Chongqing is in China.\n",
    "\n",
    "# New York is in US.\n",
    "# LA is in US."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e35a58",
   "metadata": {},
   "source": [
    "###  排列组合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e7a7e89",
   "metadata": {},
   "outputs": [],
   "source": [
    " import itertools\n",
    " data = [3, 4, 6]\n",
    "\n",
    " com_res = itertools.combinations(data, 2)\n",
    " print(list(com_res))\n",
    " # [(3, 4), (3, 6), (4, 6)]\n",
    "\n",
    " com_res = itertools.permutations(data, 2)\n",
    " print(list(com_res))\n",
    " # [(3, 4), (3, 6), (4, 3), (4, 6), (6, 3), (6, 4)]\n",
    "\n",
    " com_without_replacement_res = itertools.combinations_with_replacement(data, 2)\n",
    " print(list(com_without_replacement_res))\n",
    " # [(3, 3), (3, 4), (3, 6), (4, 4), (4, 6), (6, 6)]\n",
    "\n",
    " product_res = itertools.product(data, data)\n",
    " print(list(product_res))\n",
    " # [(3, 3), (3, 4), (3, 6), (4, 3), (4, 4), (4, 6), (6, 3), (6, 4), (6, 6)]"
   ]
  },
  {
   "cell_type": "raw",
   "id": "4adb892d",
   "metadata": {},
   "source": [
    "combinations 组合\n",
    "permutations 排列\n",
    "\n",
    "combinations_with_replacement 组合可重复\n",
    "product 排列可重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d0eb869",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import comb\n",
    "print((comb(8, 3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e9f11fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import perm\n",
    "print(perm(8, 3))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lanqiaobei",
   "language": "python",
   "name": "lanqiaobei"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
